import requests
import json
import string
import xlrd

#测试数据（跨境电商）
file_path = ('../../项目文件\压测清单数据.xlsx')

data = {}

portReleaseDt_1 = {}
class request_test():
    global data1
    data1 = json.dumps(data)
    def excel_in(self, for_num):
        print('5255555555555555555')
        print(file_path)
        data = xlrd.open_workbook(file_path)
        table = data.sheet_by_index(0)

        rltNoList = []
        portReleaseDt_2 = []
        portReleaseRlt_2 = []

        # 查看工作表
        # table = ""
        data.sheet_names()
        nrows = table.nrows
        print("全部sheet页如下：\n" + str(data.sheet_names()))
        print("总行数：" + str(table.nrows))
        print("总列数：" + str(table.ncols))

        if for_num + 1 <= nrows:
            for i in range(1, for_num + 1):
                """
                个人清单编号
                料号
                商品编码
                数量
                单位
                """
                portReleaseDt_0 = {}
                portReleaseRlt_0 = {}
                # print("-----------------------sheet页:%s-第%s行---------------------：" % (s, str(i)))
                rltNo = str.strip(table.cell(i, 0).value)
                gdsMtno = str.strip(str(table.cell(i, 1).value))
                gdecd = str.strip(str(table.cell(i, 2).value)[0:10])
                dclQty = str.strip(str(table.cell(i, 3).value))
                dclUnitcd = str.strip(str(table.cell(i, 4).value))

                if rltNo not in rltNoList:
                    portReleaseRlt_1.update({"rltNo": rltNo})
                    portReleaseRlt_0.update(portReleaseRlt_1)
                    portReleaseRlt_2.append(portReleaseRlt_0)
                    rltNoList.append(rltNo)
                portReleaseDt_1.update({"gdsMtno": gdsMtno})
                portReleaseDt_1.update({"gdecd": gdecd})
                portReleaseDt_1.update({"dclQty": dclQty})
                portReleaseDt_1.update({"dclUnitcd": dclUnitcd})
                portReleaseDt_0.update(portReleaseDt_1)
                portReleaseDt_2.append(portReleaseDt_0)
        else:
            return 1, '行数超出'
        return 0, portReleaseDt_2, portReleaseRlt_2
    def post_requests(self, for_num):
        #测试环境
        token = ''
        # url = ''
        url = ''


        for_num = for_num
        status = '1'

        excel_value = request_test().excel_in(for_num=for_num)
        if excel_value[0] == 0:
            portReleaseDt_2 = excel_value[1]
            portReleaseRlt_2 = excel_value[2]
            # if for_num >= 3:
            #     print('+***+*+*+*+*+**++**+**++*+**+*+*+')
            #     break
            # portReleaseRlt_0 = {}

            data = data1.replace("portReleaseRlt_1", json.dumps(portReleaseRlt_2))
            data = data.replace("portReleaseDt_1", json.dumps(portReleaseDt_2))

            data = data.replace('"[', '[')
            data = data.replace(']"', ']')
            data = data.replace("dclQty_1", json.dumps(for_num))
            data = data.replace("exitQuantity_1", json.dumps(for_num))

            print("=================1===================")
            try:
                # headers = {"orgId": "103903", "Content-Type": "application/json", "epToken": "%s" % token}
                headers = {"orgId": "103321", "Content-Type": "application/json", 'userId': '123', "orgArea": "L"}

                response = requests.post(url=url, headers=headers, data=data)
                print("请求信息：\n %s \n%s \n%s" % (url, headers, data))
                print("********************************")
                status = response.json().get("status")
                print("获取返回状态成功：%s" % status)

                ass_value = response.json()

                if status == "1" and response.json().get("note") == '修改成功！':
                    print(response.json().get("note"))
                elif 'token cather user fail' in str(ass_value):
                    print("当前最大支持表体数：%s" % (for_num-1))
                else:
                    # return False, ass_value, "请求错误"
                    print('++++++++++++++++++++++++++++++')
                    status = 0
                    print(ass_value)
                    print("当前最大支持表体数：%s" % (for_num-1))
            except Exception as E:
                print("当前最大支持表体数：%s" % (for_num-1))
                print(E)
                return False, E, "请求异常"
            for_num += 1
            print(for_num)


request_test().post_requests(15000)